import { Context } from '../types'
import Decorator from '../utils/decorator'
import { paramsValidater } from '../utils'

export default class BaseController {
  @Decorator.Get
  public static async query(ctx: Context, params: Record<string, any> = {}) {
    const res = await ctx.service.query(ctx, params)
    return res
  }

  @Decorator.Post
  public static async delete(ctx: Context, params: Record<string, any> = {}) {
    return await paramsValidater(
      ctx,
      async () => {
        await ctx.service.delete(ctx, params)
        return {}
      },
      params,
      [],
      ['id'],
    )
  }

  @Decorator.Post
  public static async update(ctx: Context, params: Record<string, any> = {}) {
    return await paramsValidater(
      ctx,
      async () => {
        await ctx.service.update(ctx, params)
        return {}
      },
      params,
      [],
      ['id'],
    )
  }

  @Decorator.Post
  public static async create(ctx: Context, params: Record<string, any> = {}) {
    params.create_at = new Date().toISOString()
    return await paramsValidater(
      ctx,
      async () => {
        await ctx.service.create(ctx, params)
        return {}
      },
      params,
    )
  }
}
